<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<%@ page import="com.esaheki.flashcards.dao.*"%>
<%@ page import="com.esaheki.flashcards.classes.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.text.NumberFormat"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<!-- Header -->
<c:import url="/includes/header.jsp">
<c:param name="title" value="FlashCards: Cards Table" />
</c:import>
<!-- Header -->

<div class="colmask leftmenu"> 
<div class="colright"> 
<div class="col2"> 
<!-- Column Left --> 
            
<c:import url="/tree.jsp">
</c:import>

<!-- Column Left --> 
</div>

<div class="col1wrap"> 
<div class="col1"> 

<!-- Column Right --> 
<div class="title">
<%
String type = request.getParameter("type");
String id = request.getParameter("id");
ArrayList<Card> cardlist = new ArrayList<Card>();

CardDAO cardDao = new CardDAO();
CategoryDAO catDao = new CategoryDAO();
UserDAO userDao = new UserDAO();

NumberFormat percentFormat = NumberFormat.getPercentInstance();
percentFormat.setMaximumFractionDigits(1);

String name = "";
if (type == null) {
	cardlist = cardDao.getAllCardList();
	%>
	<h2>All Cards</h2>
	<a href="test.jsp">Test</a>
	<%
}
else if (type.equals("user")) {
	cardlist = cardDao.getCardListByUserId(Integer.parseInt(id));
	User user = userDao.getUserById(Integer.parseInt(id));
	name = user.getName();
	String userScore = percentFormat.format(user.getAverageScore());
	
	if (cardlist.isEmpty()) {
%>
<h2><%=name %> - <%=userScore %></h2>
<%
	}
	else {
%>
<h2><%=name %> - <%=userScore %><a class="flashcards" href="test.jsp?type=<%=type %>&id=<%=id %>&answer=no">Flash Cards</a></h2>
<%	
	}
	%>
	<a class="greenlink" href="add.jsp?type=cat&id=<%=id %>">Add Category</a> | 
	<a href="edit.jsp?type=<%=type %>&id=<%=id %>">Edit User Name</a> | 
	<a class="redlink" href="delete.jsp?type=<%=type %>&id=<%=id %>">Delete User</a>
	<%
}
else if (type.equals("cat")) {
	cardlist = cardDao.getCardListByCategoryId(Integer.parseInt(id));
	Category cat = catDao.getCategoryById(Integer.parseInt(id));
	name = cat.getName();
	String userScore = percentFormat.format(cat.getAverageScore());
	if (cardlist.isEmpty()) {
%>
<h2><%=name %> - <%=userScore %></h2>
<%
	}
	else {
%>
<h2><%=name %> - <%=userScore %><a class="flashcards" href="test.jsp?type=<%=type %>&id=<%=id %>&answer=no">Flash Cards</a></h2>
<%	
	}
	%>
	<a class="greenlink" href="add.jsp?type=card&id=<%=id %>">Add Card</a> | 
	<a href="edit.jsp?type=<%=type %>&id=<%=id %>">Edit Category Name</a> | 
	<a class="redlink" href="delete.jsp?type=<%=type %>&id=<%=id %>">Delete Category</a>
	<%
}


%>
</div>
<table class="tdCards">
<tr class="tdheader">
<td class="tdFront">Front</td>
<td class="tdBack">Back</td>
<td class="tdScore" width="50px" align="center">Score</td>
<td class="tdEdit" width="100px" align="center">Options</td>
</tr>
<%
int i = 0;
for (Card card : cardlist) {
	String score = percentFormat.format(card.getScore());
	if (i % 2 == 0) {
%>
<tr class="even">
<%
	}
	else {
%>
<tr class="odd">
<%	
	} i++;
%>
<td class="tdFront"><%=card.getFront() %></td>
<td class="tdBack"><%=card.getBack() %></td>
<td class="tdScore" align="center"><%=score %></td>
<td class="tdEdit" align="center">
<a href="edit.jsp?type=card&id=<%=card.getId() %>">Edit</a> | 
<a class="redlink" href="delete.jsp?type=card&id=<%=card.getId() %>">Delete</a>
</td>
</tr>
<%
}
%>
</table>

<!-- Column Right --> 
</div> 
</div> 
</div> 
</div> 

<!-- Footer -->
<c:import url="/includes/footer.jsp" />
<!-- Footer -->